$(function(){
	//异步请求，更新用户信息
	$("#info .save-btn").click(function(){
		
		var userid=$("#userid").text();
		var usersex=$("#usersex").val();
		var qqcontact=$("#qqcontact").val();
		var phonecontact=$("#phonecontact").val()
		var selfsign=$("#selfsign").val();
		
		$.ajax({
			url:"userPerfect",
			method:"post",
			data:{
				"perferFlag":"a",
				"userid":userid,
				"usersex":usersex,
				"qqcontact":qqcontact,
				"phonecontact":phonecontact,
				"selfsign":selfsign
			},
			dataType:"json",
			success:(data)=>{
				
				  //激活模态框
				  $("#userModal .modal-body").text(data.msg);
				  $('#userModal').modal({
				    keyboard: false
				  })
				
			},
			error: () => {
				alert("服务异常!")
			}
		})
	})
	
	
	
	//异步请求，更新用户密码邮箱
	$("#pwd .save-btn2").click(function(){
	
		var userid=$("#userid").text();
		var oldpwd=$("#oldpwd").val();
		var newpwd=$("#newpwd").val();
		var surenewpwd=$("#surenewpwd").val()
		var email=$("#email").val();
		//检验原始密码
		if( !isOldPwd){
			//激活模态框
			  $("#userModal .modal-body").text("请输入正确的原始密码!");
			  $('#userModal').modal({
			    keyboard: false
			  })
			return;
		}
		
		//校验新密码
		if(! isSurePwd()){
			return;
		}
		
		
		$.ajax({
			url:"userPerfect",
			method:"post",
			data:{
				"perferFlag":"b",
				"userid":userid,
				"pwd":surenewpwd,
				"email":email
			},
			dataType:"json",
			success:(data)=>{
			
				  //激活模态框
				  $("#userModal .modal-body").text(data.msg);
				  $('#userModal').modal({
				    keyboard: false
				  })
				
			},
			error: () => {
				alert("服务异常!")
			}
		})
	})
	

	
	//校验原密码
   var isOldPwd=false;
   
	$("#oldpwd").blur(function(){
			var pwdUsername=$("#pwdUsername").text();
			var oldpwd=$("#oldpwd").val();
			
			 $("#oldtip").text("");
		 $.ajax({
			url:"userPerfect",
			method:"post",
			data:{
				"perferFlag":"c",
				"pwdUsername":pwdUsername,
				"oldpwd":oldpwd
			},
			dataType:"json",
			success:(data)=>{
				
				 if(data.code===0){
				    $("#oldtip").text(data.msg);
				    isOldPwd=true;
				    return false;
				 }
				return true;
			},
			error: () => {
				alert("服务异常!")
			}
		})
			
	  })
	
	//校验新密码
	function isSurePwd(){
		var newpwd=$("#newpwd").val();
		var surenewpwd=$("#surenewpwd").val()
		if(newpwd !==surenewpwd){
			  $("#suretip").text("新密码与确认新密码不一致!");
			  return false;
		  }
		 return true;
	}
	
	
	$("#surenewpwd").blur(function(){
	    $("#suretip").text("");
			isSurePwd();
	
	})
	
	
	//模态框确认，刷新网页
	$(".user-btn").click(function(){
	   top.location.reload();
	})
	
	
	
	
	//头像上传
	$(".save-btn3").click(function(){
			var userid=$("#userid").text();
	
			console.log(888888)
			var file=$("#upload").get(0).files[0];
			
			var formData=new FormData();
			formData.append("file",file);
			console.log(formData.get("file"))
			$.ajax({
				url:"photoUpLoad?userid="+userid,
				method:"post",
				data:formData,
				dataType:"json",
				cache:false, //不缓存
				processData:false,//jqueay不处理发生的数据
				contentType:false,//jqueay不处理content-Type请求头
				success:(data)=>{
				  //激活模态框
				  $("#userModal .modal-body").text(data.msg);
				  $('#userModal').modal({
				    keyboard: false
				  })
					
				},
				error: () => {
					alert("服务异常!")
				}
			})

			
		})

	
	
	
})